function getprovince(province_id,city_id,area_id){
    var province_html = '<option value="0">请选择</option>';
    var city_html = '<option value="0">请选择</option>';
    sys_city.forEach(function (item) {
        if(0==item.parent_id){
            province_html += '<option '+(province_id == item.id ? 'selected' : '')+' value="'+item.id+'">'+item.name+'</option>';
        }

        if(province_id == item.id){
            var city_val = item.son_val;
            city_val.forEach(function (item1) {
                city_html += '<option '+(city_id == item1.id ? 'selected' : '')+' value="'+item1.id+'">'+item1.name+'</option>';
            });
        }
    });
    $('#province_id').html(province_html);
    $('#city_id').html(city_html);
    getcity(province_id,city_id,area_id);
}


function getcity(province_id,city_id,area_id){
    var area_html = '<option value="0">请选择</option>';
    sys_city.forEach(function (item) {
        if(province_id == item.id){
            var city_val = item.son_val;
            city_val.forEach(function (item1) {
                if(city_id == item1.id){
                    var area_val = item1.son_val;
                    area_val.forEach(function (item2) {
                        area_html += '<option '+(area_id == item2.id ? 'selected' : '')+' value="'+item2.id+'">'+item2.name+'</option>';
                    });
                }
            });
        }
    });
    $('#area_id').html(area_html);
}

//启用禁用
$('#table_id_example').on('click','.input-check',function(){
    var id = $(this).parent().attr('data-id');
    var use_flg = $(this).parent().attr('data-flg');
    changeStatus(id,use_flg,1);
});

function changeStatus(id,status,type){
    if(type == 1){
        title = '你确定重置用户的密码吗？';
    }else{
        title = '你确定重置用户的密码吗？';
    }
    $.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/goods-keyword/change-keyword-status',{id:id,status:status,type:type},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        if(status == 1){
                $('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
            }else{
                $('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
            }
            $.jBox.close();
        }
    });
}

//重置密码
function userresetpass(mobile){
    $('#frompassword_pwd').val('');
    $('#frompassword_rePwd').val('');
    $('#frompassword_mobile').val(mobile);
    $('#userresetpass-modal').modal('show');
}

//短信发送密码给客户
function updatesendmessage(member_no,mobile){
    var title= '你确定重置用户的密码';
    $.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/cust/reset-sms-password',{mobile:mobile},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200) {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
        }
    });
}

$(document).ready(function(){
    $('#from_birthday').datepicker({
        format: 'yyyy-mm-dd'
    });
    //datatable
    $('#table_id_example').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        //add by allen.qiang 20180130 优化会员查询页面布局  start
        "scrollX": true,
        //add by allen.qiang 20180130 优化会员查询页面布局    end
        ajax:{
            url:'/cust/list',
            type:'post',
            data:function(d){
                d.member_no = $('#member_no').val();
                d.member_name = $('#member_name').val();
                d.mobile = $('#mobile').val();
                d.reservationtime = $('#reservationtime').val();
            }
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "username", "bSortable": true},
            { "data": "member_no", "bSortable":true},
            { "data": "mobile", "bSortable": false },
            { "data": "sex", "bSortable": false },
            { "data": "email", "bSortable": false },
            { "data": "birthday", "bSortable": true },
            { "data": "consum_amount", "bSortable": false },
            { "data": "current_points", "bSortable":false },
            { "data": "source", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    //del by allen.qiang 20180131 删除调试代码 start
                    //console.log(full);
                    //del by allen.qiang 20180131 删除调试代码   end
                    return '<a href="javascript:" onclick="changeusercenter(' + full.member_no +','+ full.province_code +','+ full.city_code +','+ full.district_code +','+ full.mobile +','+ full.sex +','+'\''+ full.username +'\''+','+'\''+ full.birthday +'\''+','+'\''+ full.adddress +'\''+','+'\''+ full.email +'\''+','+'\''+ full.adddress +'\''+')">编辑</a> &nbsp;&nbsp;&nbsp;' +
                            '<a href="javascript:" onclick="updatesendmessage('+ full.member_no +','+ full.mobile +')">重置密码（短信）</a>&nbsp;&nbsp;&nbsp; ' +
                            '<a href="javascript:" onclick="userresetpass('+ full.mobile +')">手工重置密码</a> &nbsp;&nbsp;&nbsp;';
                }
            },
            {
                "targets": [3],
                "searchable":false
            },
            {
                "targets": [4],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return (data==1) ?'男':'女';
                }
            },
            //add by FDG367 491 【后台管理】会员查询页面，用户来源显示调整 start
            {
                "targets": [9],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    var str ='未知';
                    if(1==data){
                        str = '商城注册PC';
                    }else if(2==data){
                        str = '线下同步';
                    }else if(3==data){
                        str = '商城WAP';
                    }
                    return str;
                }
            },
            //add by FDG367 491 【后台管理】会员查询页面，用户来源显示调整 end
        ],
        order: [[ 1, "desc" ]]
    });
});

//修改个人中心资料弹出层
function changeusercenter(member_no,province_code,city_code,district_code,mobile,sex,username,birthday,adddress,email){
    var type =1;
    if(type == 1){
        title = '你确定重置用户的密码';
    }else{
        title = '你确定重置用户的密码？';
    }
    birthday  =  birthday.substr(0,10);
    $('#from_birthday').val(birthday);

    $('input[name="member_no_disabled"]').val(member_no);
    $('#form_member_no').val(member_no);
    $('#form_mobile').val(mobile);
    $('#from_username').val(username);
    $('#from_adddress').val(adddress);
    $('#from_email').val(email);
    $("#form_sex").find("option").eq(sex).attr("selected","selected")

    pcaArea(province_code,city_code,district_code);


    $('#usercenter-modal').modal('show');
}
$('#reservationtime').daterangepicker({
    language:  'zh-CN',
    timePicker: true,
    timePickerIncrement: 30,
    format: 'YYYY/MM/DD H:i:s',
    locale: {
    format: 'YYYY-MM-DD HH:mm:ss',
    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
    }
    });

$('#select_button').click(function(){
    table_list_reload();
    })

//table刷新
function table_list_reload(){
    var table = $('#table_id_example').DataTable();
    table.ajax.reload();
    }

/**
 *@param  处理日期方法
 */
function pcaArea(province_id,city_id,area_id){
    if(province_id){
        getprovince(province_id,city_id,area_id);
    }

    $('#province_id').on('change',function(){
        city_id	= $('#city_id').val();
        area_id	= $('#area_id').val();
        getprovince($(this).val(),city_id,area_id);
    });

    $('#city_id').on('change',function(){
        province_id	= $('#province_id').val();
        area_id	= $('#area_id').val();
        getcity(province_id,$(this).val(),area_id);
    });
}

/**
 * @param 更新用户数据
 */
$('.ajax_post_update').on('click',function(){
    var url  = $('#update_cust_master').attr('action');
    var form_serialize = $("#update_cust_master").serialize();
    $.post(url,form_serialize,success,'json');
    return false;
    function success(data){
        if(data.code == 200) {
            $.jBox.tip('处理成功', 'success');
            $('#usercenter-modal').modal('hide')
            window.setTimeout(function () { table_list_reload(); }, 1000);
        }
        else{

            $.jBox.error(data.val, '错误');
        }
    }
})

/**
 * @param 更新密码
 */
$('.ajax_post_password').on('click',function(){
    var url  = $('#reset_password').attr('action');
    var form_serialize = $("#reset_password").serialize();

    $.post(url,form_serialize,success,'json');
    return false;
    function success(data){
        if(data.code == 200) {
            $.jBox.tip('处理成功', 'success');
            $('#userresetpass-modal').modal('hide')
            window.setTimeout(function () { table_list_reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
});

$('#reset_button').on('click',function(){
	$('#member_no').val('');
	$('#member_name').val('');
	$('#mobile').val('');
	$('#reservationtime').val('');
	table_list_reload();
});



